package com.ruoyi.match.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.RandomUtil;
import com.ruoyi.match.service.IMatchEquipmentService;
import com.ruoyi.match.service.MatchUserService;

import com.ruoyi.match.domain.MatchEquipment;
import com.ruoyi.match.domain.MatchUserVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WYC
 * @since 2021-03-18
 */
@RestController
@RequestMapping("/match/user")
@Api(tags = "matchuser")
public class MatchUserController {
    @Autowired
    private MatchUserService matchUserService;
    @Autowired
    private IMatchEquipmentService matchEquipmentService;
    @ApiOperation(value = "根据条件筛选获取满足条件的所有作业记录", notes = "根据条件筛选获取满足条件的所有作业记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true ,dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码",required = true , dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "headImg", value = "头像",required = true , dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "yzm", value = "验证码",required = true , dataType = "String", paramType = "form")

    })
    @PostMapping(value = "app/register",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public AjaxResult add(@RequestParam(value = "phone",required = true) String phone,
                          @RequestParam(value = "password",required = true) String password,
                          @RequestParam(value = "yzm",required = true) String yzm,
                          @RequestParam(value = "headImg",required = true) MultipartFile headImg,
                          HttpServletRequest request
                            )
    {

            matchUserService.addUser(phone,password,yzm,headImg,request);

            return AjaxResult.success();


    }
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true ,dataType = "String", paramType = "path"),

    })
    @GetMapping("send/{phone}")
    public AjaxResult sendMsm(@PathVariable String phone, HttpServletRequest request) {

        String code = RandomUtil.getFourBitRandom();
        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        //调用service发送短信的方法
        boolean isSend = matchUserService.send(param,phone);
        if(isSend) {
            //发送成功，把发送成功验证码放到redis里面
            //设置有效时间
            request.getSession().setAttribute("CodePhone",code);
            return AjaxResult.success();
        } else {
            return AjaxResult.error("短信发送失败");
        }
    }

    @ApiOperation(value = "app登录", notes = "app登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true ,dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码",required = true , dataType = "String", paramType = "form")

    })
    @PostMapping(value ="/app/login",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public AjaxResult login(@RequestParam(value = "phone",required = true) String phone,
                          @RequestParam(value = "password",required = true) String password)
    {

            matchUserService.login(phone,password);
            return AjaxResult.success();

    }
    @ApiOperation(value = "用户申请绑定", notes = "")
    @PostMapping(value = "/app/bangding",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public AjaxResult bangding(@RequestParam(value = "phone",required = true) String phone,
                            @RequestParam(value = "equipmentId",required = true) String equipmentId)
    {

            matchUserService.bangding(phone,equipmentId);

        return AjaxResult.success();
    }
    @ApiOperation(value = "用户解除绑定", notes = "")
    @PostMapping(value = "/app/jiechubangding",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public AjaxResult jiechubangding(@RequestParam(value = "phone",required = true) String phone)
    {

        matchUserService.jiechu(phone);

        return AjaxResult.success();
    }
    @ApiOperation(value = "修改资料", notes = "修改资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true ,dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "headImg", value = "头像", dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "int", paramType = "form")

    })
    @PostMapping(value = "app/update",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public AjaxResult add(@RequestParam(value = "phone",required = true) String phone,
                          @RequestParam(value = "password",required = false) String password,
                          @RequestParam(value = "name",required = false) String name,
                          @RequestParam(value = "headImg",required = false)MultipartFile headImg,
                          @RequestParam(value = "age",required = false)Integer age,
                          @RequestParam(value = "sex",required = false)Integer sex

    )
    {

        matchUserService.update(phone,password,name,headImg,age,sex);

        return AjaxResult.success();


    }
    @ApiOperation(value = "查询资料", notes = "查询资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true ,dataType = "String", paramType = "path"),

    })
    @GetMapping("app/findPhone/{phone}")
    public AjaxResult findPhone(@PathVariable String phone) {
       MatchUserVo user= matchUserService.findByPhone(phone);

        return AjaxResult.success(user);
    }

    @ApiOperation(value = "查询未绑定的设备", notes = "查询未绑定的设备")

    @GetMapping("app/getAllEquiment")
    public AjaxResult getAll(){
        List<MatchEquipment> list = matchEquipmentService.selectAllWei(0);

            return AjaxResult.success(list);


    }
}

